#include "cadrepostdialog.h"
#include "ui_cadrepostdialog.h"
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<QCompleter>
#include"qualifydocu.h"
#include"sqlconnect.h"
cadrepostDialog::cadrepostDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::cadrepostDialog)
{
    ui->setupUi(this);
    QStringList strings2;
    QSqlQuery query2("SELECT unitname FROM unit ORDER BY unitid");
    while (query2.next()) {
    QString nation = query2.value(0).toString();
    strings2.append(nation);
    }
    QCompleter* completer = new QCompleter(strings2, this);
    ui->unit->clear();
    ui->unit->addItems(strings2);
    ui->unit->setEditable(true);
    ui->unit->setCompleter(completer);
    connect(ui->cancel,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->confirm,SIGNAL(clicked(bool)),this,SLOT(react()));
    connect(ui->confirm,SIGNAL(clicked(bool)),parent,SLOT(showPostInfo()));
    //connect(,SIGNAL(sendID(QString)),post_dialog,SLOT(reciveID(QString)));
}

cadrepostDialog::~cadrepostDialog()
{
    delete ui;
}
void cadrepostDialog::react(){
    QString unit=ui->unit->currentText();
    QString unitid;
    QSqlQuery query4;
    query4.exec("SELECT unitid FROM ZZB.unit where unitname='"+unit+"'");
    while (query4.next()) {
    unitid = query4.value(0).toString();
    }
    QString post=ui->postEdit->text();
    if(post.count()==0){
        QMessageBox::information(this,"提示信息","职务不能为空！");
        return;
        }

    //任免类型QualifyType：现任0 /拟任1 /拟免2
    QString qualifyType=ui->qualifyType->currentText();
    //qDebug()<<unit<<" "<<qualifyType;
    QString type;
    if(qualifyType==QString::fromLocal8Bit("现任")){
        type="0";
    }
    if(qualifyType==QString::fromLocal8Bit("拟任")){
        type="1";
    }
    if(qualifyType==QString::fromLocal8Bit("拟免")){
        type="2";
    }
    //sqlconnect(db);
    QSqlQuery qu("select PostID from Post where PostName='"+post+"' and QualifyType='"+type+"' and IDNum='"+ID+"'");
    if(qu.next())
    {
        QMessageBox::information(this,"提示信息","职务已存在，不可重复添加！");
        return;
    }
    else{
        QSqlQuery query;
        QString sql="INSERT INTO Post(PostName,QualifyType,IDNum,UnitID)VALUES('"+post+"','"+type+"','"+ID+"','"+unitid+"')";
        bool ok=query.exec(sql);
        //qDebug()<<ok;
        if(ok){
            QMessageBox::information(this,"提示信息","成功插入!");
        }
     }
    //db.close();
    this->close();
}
void cadrepostDialog::reciveID(QString s){
    ID=s;
}
